﻿using System.IO;
using System.Diagnostics;
using System;
using System.Windows.Forms;

namespace DotNet.Common
{
	static public class CommonApp
	{
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fatherForm"></param>
        /// <param name="fileFilter">eg, Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*</param>
        /// <param name="createFileFunc"></param>
		public static void RunNewFileMenu(Form fatherForm, string fileFilter, FileInvoker createFileFunc)
		{
			RunSaveFileDialog(fatherForm, "新建", fileFilter + "|所有文件(*.*)|*.*", createFileFunc);
		}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fatherForm"></param>
        /// <param name="fileFilter">eg, Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*</param>
        /// <param name="openFileFunc"></param>
		public static void RunOpenFileMenu(Form fatherForm, string fileFilter, FileInvoker openFileFunc)
		{
			RunOpenFileDialog(fatherForm, null, fileFilter, openFileFunc);
		}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileFilter">eg, Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*</param>
        /// <param name="saveFileFunc"></param>
		public static void RunSaveFileMenu(Form fatherForm, string fileFilter, FileInvoker saveFileFunc)
		{
			RunSaveFileDialog(fatherForm, null, fileFilter, saveFileFunc);
		}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fatherForm"></param>
        /// <param name="title"></param>
        /// <param name="fileFilter">eg, Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*</param>
        /// <param name="openFileFunc"></param>
		public static void RunOpenFileDialog(Form fatherForm, string title, string fileFilter, FileInvoker openFileFunc)
		{
			Debug.Assert(fatherForm != null && fileFilter != null && openFileFunc != null);

			using (OpenFileDialog oDialog = new OpenFileDialog())
			{
				if (!string.IsNullOrEmpty(title))
				{
					oDialog.Title = title;
				}
				oDialog.Filter = fileFilter;
				if (oDialog.ShowDialog(fatherForm) == DialogResult.OK)
				{
					openFileFunc(oDialog.FileName);
				}
			}
		}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fatherForm"></param>
        /// <param name="title"></param>
        /// <param name="fileFilter">eg, Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*</param>
        /// <param name="saveFileFunc"></param>
		public static void RunSaveFileDialog(Form fatherForm, string title, string fileFilter, FileInvoker saveFileFunc)
		{
			Debug.Assert(fatherForm != null && fileFilter != null && saveFileFunc != null);

			using (SaveFileDialog oDialog = new SaveFileDialog())
			{
				if (!string.IsNullOrEmpty(title))
				{
					oDialog.Title = title;
				}
				oDialog.Filter = fileFilter;
				if (oDialog.ShowDialog(fatherForm) == DialogResult.OK)
				{
					saveFileFunc(oDialog.FileName);
				}
			}
		}

        public static void RunSelectFolderDialog(Form fatherForm,string description,StringInvoker selectFolderFunc)
        {
            Debug.Assert(fatherForm != null && selectFolderFunc != null);

            using (FolderBrowserDialog oDialog = new FolderBrowserDialog())
            {
                oDialog.Description = description;
                if (oDialog.ShowDialog(fatherForm) == DialogResult.OK)
                {
                    selectFolderFunc(oDialog.SelectedPath);
                }
            }
        }

		public static void RunExitMenu(Form fatherForm)
		{
			Debug.Assert(fatherForm != null);

			fatherForm.Close();
		}

		public static void RunHelpMenu(string helpFilepath)
		{
			Process.Start(System.AppDomain.CurrentDomain.BaseDirectory + helpFilepath);
		}

		public static void RunAboutMenu(Form fatherForm, Form aboutForm)
		{
			aboutForm.ShowDialog(fatherForm);
		}

		public static void SelectExplorerFile(string filepath)
		{
			if (File.Exists(filepath))
			{
				Process.Start(Environment.GetFolderPath(System.Environment.SpecialFolder.Windows) + @"\explorer.exe", string.Format(@"/n,/select,{0}", filepath));
			}
		}

		public static void RunErrorMessageBox(Form fatherForm, string message)
		{
			MessageBox.Show(fatherForm, message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
		}

        public static void RunWarningMessageBox(Form fatherForm, string message)
        {
            MessageBox.Show(fatherForm, message, "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

		public static bool RunVerifyMessageBox(Form fatherForm, string message)
		{
			return (MessageBox.Show(fatherForm, message, "请确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK);
		}

		public static void RunInformationMessageBox(Form fatherForm, string message)
		{
			MessageBox.Show(fatherForm, message, "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

		public static bool RunDosExeFile(string exeFile, string arg)
		{
			try
			{
				Process oNewProcess = new Process();
				oNewProcess.StartInfo.FileName = exeFile;
				oNewProcess.StartInfo.Arguments = arg;
				oNewProcess.StartInfo.UseShellExecute = false;
				oNewProcess.StartInfo.CreateNoWindow = true;
				oNewProcess.StartInfo.RedirectStandardOutput = true;
				oNewProcess.StartInfo.RedirectStandardError = true;
				oNewProcess.Start();

				oNewProcess.WaitForExit();
				return (oNewProcess.ExitCode == 0);
			}
			catch
			{
				return false;
			}
		}

		public static bool RunDosExeFile(string exeFile, string arg,out string output)
		{
			try
			{
				Process oNewProcess = new Process();
				oNewProcess.StartInfo.FileName = exeFile;
				oNewProcess.StartInfo.Arguments = arg;
				oNewProcess.StartInfo.UseShellExecute = false;
				oNewProcess.StartInfo.CreateNoWindow = true;
				oNewProcess.StartInfo.RedirectStandardOutput = true;
				oNewProcess.Start();

				oNewProcess.WaitForExit();
				output = oNewProcess.StandardOutput.ReadToEnd();
				return (oNewProcess.ExitCode == 0);
			}
			catch
			{
				output = string.Empty;
				return false;
			}
		}

		public static bool RunWinExeFile(string exeFile, string arg)
		{
			try
			{
				Process oNewProcess = new Process();
				oNewProcess.StartInfo.FileName = exeFile;
                if (!string.IsNullOrEmpty(arg))
                {
                    oNewProcess.StartInfo.Arguments = arg;
                }
				oNewProcess.Start();

				return true;
			}
			catch
			{
				return false;
			}
		}

        public static bool RunWinExeFile(string exeFile)
        {
            return RunWinExeFile(exeFile, null);
        }
	}
}
